package com.sq.goodline.task;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.sq.goodline.service.MailService;
import com.sq.goodline.service.PushToPartnerService;
import com.sq.goodline.service.RecService;
import com.sq.goodline.service.RecToWavService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@EnableScheduling
public class VosVidioTask {

    @Autowired
    private MailService mailService;
    @Autowired
    RecToWavService recToWavService;
    @Autowired
    RecService recService;
    
    @Autowired
	PushToPartnerService pushToPartnerService;
    
    /**
     * 录音URL推送给客户方
     */
    @Scheduled(cron = "${job.pushAudioUrlTaskCron}")
    private void pushAudioUrlTask(){
    	String traceId = UUID.randomUUID().toString().replace("-","");
		MDC.put("TraceId", traceId);//日志追踪
    	String mailSubject="录音URL推送定时任务";
		StringBuffer mailContext = new StringBuffer();
    	try {
    		log.debug("录音URL推送任务开始...");
    		pushToPartnerService.pushAudioDetail();
    	} catch (Exception e) {
			log.error("录音URL推送Task异常",e);
			mailContext.append("录音URL推送定时任务执行异常:\r\n  失败原因："+e.getMessage());
			mailService.sendSimpleMail(mailSubject, mailContext.toString());
		}finally {
			log.debug("录音URL推送任务完毕");
		}
    }
    
    
    /**
     * 话单数据推送
     * 接收udp消息后，会单笔实时推送一次
     * 这里是补推送 
     */
    @Scheduled(cron = "${job.pushAganTaskCron}")
    private void pushAganTask(){
    	String traceId = UUID.randomUUID().toString().replace("-","");
		MDC.put("TraceId", traceId);//日志追踪
    	String mailSubject="补推送定时任务";
		StringBuffer mailContext = new StringBuffer();
    	try {
    		log.debug("话单数据补推送任务开始...");
    		pushToPartnerService.pushCallDetail();
    	} catch (Exception e) {
			log.error("补推送Task异常",e);
			mailContext.append("补推送定时任务执行异常:\r\n  失败原因："+e.getMessage());
			mailService.sendSimpleMail(mailSubject, mailContext.toString());
		}finally {
			log.debug("补推送任务完毕");
		}
    }
    /**
     * 录音文件rec关联话单数据
     */
    @Scheduled(cron = "${job.recRelaCallDetailCron}")
    private void recRelaCallDetailTask(){
    	String traceId = UUID.randomUUID().toString().replace("-","");
		MDC.put("TraceId", traceId);//日志追踪
    	String mailSubject="录音文件rec关联话单定时任务";
		StringBuffer mailContext = new StringBuffer();
    	try {
    		log.debug("rec关联话单任务开始...");
    		mailContext = recService.recRelaCallDetail();
    	} catch (Exception e) {
			log.error("rec关联话单Task异常",e);
			mailContext.append("rec关联话单定时任务执行异常:\r\n  失败原因："+e.getMessage());
		}finally {
			if(StringUtils.isNotBlank(mailContext.toString())) {
    			mailService.sendSimpleMail(mailSubject, mailContext.toString());
    		}
			log.debug("rec关联话单任务完毕");
		}
    }
    
    /**
     * 本地rec上传OSS
     */
    @Scheduled(cron = "${job.recUploadOssTaskCron}")
    private void recUploadOssTask(){
    	String traceId = UUID.randomUUID().toString().replace("-","");
		MDC.put("TraceId", traceId);//日志追踪
    	String mailSubject="rec上传OSS定时任务";
		StringBuffer mailContext = new StringBuffer();
    	try {
    		log.debug("rec上传OSS任务开始...");
    		mailContext = recService.recUploadOss();
    	} catch (Exception e) {
			log.error("rec上传OSS异常",e);
			mailContext.append("rec上传OSS定时任务执行异常:\r\n  失败原因："+e.getMessage());
		}finally {
			if(StringUtils.isNotBlank(mailContext.toString())) {
    			mailService.sendSimpleMail(mailSubject, mailContext.toString());
    		}
			log.debug("rec上传OSS任务完毕");
		}
    }
    
    /**
     * 待启用：
     * 确保oss上存在录音后，再删除本地录音原始文件rec 
     */
    
    
    
    
    /**
     * 弃用
     * 录音文件rec转wav再上传oss
     */
/*    @Scheduled(cron = "${job.recToWavTaskCron}")
    private void recToWavTask(){
    	String traceId = UUID.randomUUID().toString().replace("-","");
		MDC.put("TraceId", traceId);//日志追踪
    	String mailSubject="rec转wav定时任务";
		StringBuffer mailContext = new StringBuffer();
    	try {
    		log.debug("recToWav任务开始...");
    		recToWavService.recToWav();
    	} catch (Exception e) {
			log.error("recToWavTask异常",e);
			mailContext.append("录音rec转wav定时任务执行异常:\r\n  失败原因："+e.getMessage());
			mailService.sendSimpleMail(mailSubject, mailContext.toString());
		}finally {
			log.debug("recToWav任务完毕");
		}
    }
 */   
  
}
